﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using blackhouse.BlackApart.Business;
using blackhouse.BlackApart.Business.ApartmentServices;
using blackhouse.BlackApart.Business.Reservations;
using blackhouse.BlackApart.Web.Modules;
using blackhouse.Currencies;
using blackhouse.JavaScript.JQuery;

namespace blackhouse.Aparts.Web.Modules {
    public partial class AdditionalServiceEditablePerNight : System.Web.UI.UserControl, IReservationAdditionalServiceView {

        #region Fields

        private IWebReservation reservation;
        private IAdditionalService service;
        public int Days { get; set; }
        public Currency Value { get; set; }
        public Currency UnitPrice { get; set; }
        /// <summary>
        /// Określa, czy wyświetlić opcje dla większej liczby dni. 
        /// </summary>
        private bool extendedMultipleDays;

        #endregion

        #region Methods

        void IReservationAdditionalServiceView.Load(IWebReservation webReservation, IAdditionalService service) {
            this.reservation = webReservation;
            this.service = service;
            this.SetInstance();
            this.PopulateControls();
        }

        public void RegisterClientUI(Panel pnlLine) {
            this.selectedVariant.Attributes.Add("ClientUI", pnlLine.ClientID);
            this.quanntityForAll.Value = this.Days.ToString();
        }

        private void SetInstance() {
            this.extendedMultipleDays = this.reservation.DepartureDate - this.reservation.ArrivalDate > TimeSpan.FromDays(1);
        }

        private void PopulateControls() {
            this.ltlLid.Text = this.service.Description;
            this.ltlApartmentLid.Text = this.service.ApartmentDescription;
            ListItem li = this.selectedVariant.Items.FindByValue("forAll");
            if (li != null)
                li.Text = this.GetTextOnActivateService();
            if (this.service.PriceOnDemand > 0)
                this.ltlPriceOnDemand.Value = this.service.PriceOnDemand;
            else
                this.pnlPriceOnDemand.Visible = false;
            this.ltlUnitPrice.Text = this.UnitPrice.ToString();
            this.daysTable.FromDate = this.reservation.ArrivalDate;
            this.daysTable.ToDate = this.reservation.DepartureDate;
            if (this.service.ServiceTime == ServiceTime.Morning)
                this.daysTable.FromDate = this.daysTable.FromDate.AddDays(1);
            else
                this.daysTable.ToDate = this.daysTable.ToDate.AddDays(-1);
            this.daysTable.RaiseLoad();

            int apCount = this.reservation.ApartmentReservation.Length;
            if (apCount > 1) {
                this.trCalc.Visible = true;
                this.ltlApartmentCount.Text = apCount.ToString();
            }

            if (!string.IsNullOrEmpty(this.service.ReviewTitle)) {
                this.lblReviewTitle.Text = this.service.ReviewTitle + ":";
                this.pnlClientReview.Visible = true;
            }
            this.AddJavaScript();
        }

        private string GetTextOnActivateService() {
            StringBuilder sb = new StringBuilder(HttpContext.GetGlobalResourceObject("Aparts", "orderService1").ToString());
            sb.Append(this.Value);
            sb.Append("</b>");
            if (this.extendedMultipleDays) {
                sb.AppendFormat(" ({0} " + HttpContext.GetGlobalResourceObject("Aparts", "days") + " * {1})", this.Days, this.UnitPrice);
            }
            return sb.ToString();
        }

        private void AddJavaScript() {
            JQueryLibraries jql = new JQueryLibraries();
            // jql.IncludeJQueryCheckboxes();
        }

        public void ReceiveSave() {
            ReservationAdditionalService rService = this.service.AddToReservation(this.reservation);
            if (rService != null) {
                switch (Request[this.selectedVariant.UniqueID]) {
                    case "forAll":
                        this.BookForAll(rService);
                        break;
                    case "perDay":
                        this.BookPerDays(rService);
                        break;
                }
                if (this.txtReview.Visible)
                    rService.ClientReview = Request[this.txtReview.UniqueID];
                rService.Save();
            }
        }

        private void BookForAll(ReservationAdditionalService rService) {
            DateTime st = this.reservation.ArrivalDate.Date;
            DateTime et = this.reservation.DepartureDate.AddDays(-1).Date;
            if (rService.Service.ServiceTime == ServiceTime.Morning) {
                st = st.AddDays(1);
                et = et.AddDays(1);
            }
            while (st <= et) {
                rService.BookService(st);
                st = st.AddDays(1);
            }
        }

        private void BookPerDays(ReservationAdditionalService rService) {
            Dictionary<DateTime, int> userDays = this.daysTable.GetDayValues();
            foreach (KeyValuePair<DateTime, int> item in userDays) {
                rService.BookService(item.Key);
            }
        }

        #endregion

    }
}